/*
 * set_led.c
 *
 *  Created on: Jun 27, 2011
 *      Author: yzha
 */

#include "stdio.h"
#include "stdlib.h" //system
#include "string.h"
#include <assert.h>

#include "property.h"
#include "init_appl.h"
#define ZO '0'
#define PO '1'


void uint8tobin(unsigned char in){
	unsigned char base;
	int i;
	base = in;
	for(i=7;i>=0;--i){
		base = in;
		int tmp = base >> i;
		putchar(tmp?PO:ZO);
		in -= tmp<<i;
	}
}


void sub_led(uint8_t *value,int len)
{
	printf("****");
	int i =0; //for loop
	for(i=0;i<len;i++){
		printf("%d",value[i]);
	}
	printf("**** \n");
}


int display_dmem(){
	FILE * in = fopen(dmemLoc,"r");
	uint8_t p;
	if(!in){
		perror("fin");
		return 1;
	}
	while (feof(in) == 0)
	{
		if(!fread(&p,sizeof(uint8_t),1,in)){
			perror("fread");
			return 1;
		}
	}
	if(fclose(in)){
		perror("fclose");
		return 1;
	}
	return 0;
}

int write_dmem(uint8_t *value,int len,uint32_t address){
	FILE *out = fopen(dmemLoc[address],"w");
	if(!out){
		perror("fopen");
		return 1;
	}
	int i =0; //for loop
	if(!fwrite(&value ,sizeof(uint8_t),len,out)){
		perror("fwrite");
		return 1;
	}
	if(fclose(out)){
		perror("fclose");
		return 1;
	}
	return 0;
}

int set_led(dmp_property_map_t *prop_map)
{
	assert(prop_map);
	assert(prop_map->value);
	printf("address is %X \n",prop_map->property.address);
	write_dmem(prop_map->value,prop_map->size,prop_map->property.address);
	return 0;
}





